package com.simtop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.Operate;
import com.simtop.service.OperateService;
import com.simtop.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author lvyongwei
 */
@CrossOrigin
@Controller
@RequestMapping("/operate")
public class OperateController {

    @Autowired
    private OperateService operateService;

    @Autowired
    private ExcelUtils excelUtils;

    /**
     * 获取记录的日志信息
     * @return
     */
    @RequestMapping("/getOperate")
    @ResponseBody
    public ServerResponse<Object> getOperate(@RequestBody Operate operate){
        try{
            PageHelper.startPage(operate.getPageNum(),operate.getPageSize());
            List<Operate> operates = operateService.getOperate(operate);
            PageInfo<Operate> pageInfo = new PageInfo<Operate>(operates);
            return ServerResponse.createBySuccess("获取成功",pageInfo);
        }catch (Exception e){
            return ServerResponse.createByErrorMsg("获取失败");
        }
    }


    @GetMapping("/exportOperate")
    public void exportOperate(HttpServletRequest request, HttpServletResponse response){
        List<Operate> operates = operateService.getOperate(null);
        excelUtils.cooperationData(request,response,operates,"日志文件",Operate.class);
    }
}
